﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ExpertMIS.Brokers;
using ExpertMIS.Entities;

namespace ExpertMIS.Columns
{
    public partial class AllColumnList : System.Web.UI.Page
    {
        private bool firstOne = true;
        private ADMColumnBroker _columnBroker = new ADMColumnBroker();
        private ADMArticleBroker _articleBroker = new ADMArticleBroker();

        private void RefreshColumns()
        {
            dlColumns.DataSource = _columnBroker.GetAllColumns();
            dlColumns.DataBind();
        }

        private void RefreshArticle(string columnOID)
        {
            dlArticle.DataSource = _articleBroker.GetArticlesByColumn(columnOID, -1);
            dlArticle.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            RefreshColumns();
        }

        protected void dlColumns_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HyperLink HyperLink1 = e.Item.FindControl("HyperLink1") as HyperLink;
                ADMColumnEntity column = e.Item.DataItem as ADMColumnEntity;
                if (HyperLink1 != null && column != null)
                {
                    HyperLink1.NavigateUrl = "?coloid=" + column.OID;
                    if (string.IsNullOrEmpty(Request["coloid"]) && firstOne)
                    {
                        HyperLink1.CssClass = "selectedcolumn";
                        RefreshArticle(column.OID);
                    }
                    else
                    {
                        if (column.OID.Equals(Request["coloid"]))
                        {
                            HyperLink1.CssClass = "selectedcolumn";
                            RefreshArticle(column.OID);
                        }
                        else
                        {
                            HyperLink1.CssClass = "unselectedcolumn";
                        }
                    }
                }

                firstOne = false;
            }
        }

        protected void dlArticle_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Image imgArticle = e.Item.FindControl("imgArticle") as Image;
                ADMArticleEntity article = e.Item.DataItem as ADMArticleEntity;
                if (imgArticle != null && article != null)
                {
                    //if (_articleBroker.IsNewArticel(article))
                    //{
                    //    imgArticle.ImageUrl = "~/imgs/articel_new.jpg";
                    //}
                    //else if (_articleBroker.IsHotArticel(article))
                    //{
                    //    imgArticle.ImageUrl = "~/imgs/articel_hot.jpg";
                    //}
                    //else
                    //{
                    //    imgArticle.ImageUrl = "~/imgs/articel.gif";
                    //}
                }
            }
        }
    }
}
